import { useMessage } from '@hooks/useAppMessage'
import { useAxios } from '@hooks/useAxios'
import { API_ABOUT_URL } from '@services/baseApi'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'

/**
 * 图文列表
 *
 * @param {object} params
 * @param {string} params.type 分类 company/sounds
 * @returns
 */
export const useAboutQuery = (params) => {
  const { http } = useAxios()
  return useQuery({
    queryKey: ['about.list', params],
    queryFn: async () => {
      const { data } = await http.get(API_ABOUT_URL, { params })
      return data
    },
  })
}

export const useAboutUpdateMutation = () => {
  const { http } = useAxios()
  const queryClient = useQueryClient()
  const message = useMessage()
  return useMutation({
    mutationFn: async (data) => await http.put(`${API_ABOUT_URL}/${data.id}`, data),
    onSuccess: () => {
      queryClient.invalidateQueries(['about.list'])
      message.success('更新成功')
    },
  })
}

export const useAboutCreateMutation = () => {
  const { http } = useAxios()
  const queryClient = useQueryClient()
  const message = useMessage()
  return useMutation({
    mutationFn: async (data) => await http.post(`${API_ABOUT_URL}`, data),
    onSuccess: () => {
      queryClient.invalidateQueries(['about.list'])
      message.success('保存成功')
    },
  })
}

export const useAboutDeleteMutation = () => {
  const { http } = useAxios()
  const queryClient = useQueryClient()
  const message = useMessage()
  return useMutation({
    mutationFn: async (data) => await http.delete(`${API_ABOUT_URL}/${data.id}`),
    onSuccess: () => {
      queryClient.invalidateQueries(['about.list'])
      message.success('删除成功')
    },
  })
}
